classes = [
    {"name": "abyss", "age": 25, "grade": "A"},
    {"name": "kin", "age": 23, "grade": "B"},
    {"name": "tian", "age": 24, "grade": "B"},
    {"name": "hun", "age": 26, "grade": "A"},
    {"name": "exe", "age": 21, "grade": "A"}
]


# 创建常规函数：排序前准备
def sorted_2(x):
    return x["age"]


# 创建常规函数进行age+1
def map_2():
    for i in range(len(classes)):
        a = classes[i]["age"] + 1
        classes[i].update({"age": a})
        # classes[i]["age"]= a  #方法2
    return classes


# 斐波那契函数
# f(x)=f(x-1)+f(x-2)
# [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def fibonacci(x):
    if x < 0:
        return None
    if x < 2:
        return x
    return fibonacci(x - 1) + fibonacci(x - 2)


def fib(x):
    list_1 = []
    for i in range(x + 1):
        list_1.append(fibonacci(i))
    return list_1


if __name__ == '__main__':
    # 匿名函数排序
    classes_sorted_1 = sorted(classes, key=lambda x: x["age"], reverse=False)
    # 常规函数排序
    classes_sorted_2 = sorted(classes, key=sorted_2, reverse=True)

    print(f"排序:\n{classes_sorted_2}\n")

    # 常规函数age+1
    print(f"未调用age+1常规函数map_2的classes:\n{classes}")
    print(f"调用age+1常规函数map_2:\n{map_2()}")
    print(f"调用age+1常规函数map_2后classes:\n{classes}\n")

    # 匿名+map函数:age+1
    classes_map_1 = list(map(lambda x: x.update({"age": x["age"] + 1}), classes))
    print(f"调用age+1匿名函数,update返回None,因此此时返回各dict均为None:\n{classes_map_1}")
    print(f"调用age+1匿名函数后classes:\n{classes}")

    # 通过`filter`函数过滤掉上述同学中`grade`是`A`的同学
    classes_filter_1 = list(filter(lambda x: x['grade'] != "A", classes))
    print(f"调用筛选grade为A匿名函数后classes:\n{classes_filter_1}\n")

    # 使用递归来重构斐波那契函数
    y = int(input('请输入斐波那契终项序号：\n'))
    print(f"重构斐波那契:\n{fib(y)}")
    print(f"斐波那契终值:\n{fibonacci(y)}")
